﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Ship.DaDa
{
    public class HttpHelper
    {
        #region post发送数据及结果
        public static string GetHtml(string postUrl, string paramData)
        {
            string result = string.Empty;
            HttpWebRequest request = null;
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
                request = (HttpWebRequest)WebRequest.Create(postUrl);
                request.ContentType = "application/json;charset=UTF-8";
                request.Timeout = 20000;
                request.ServicePoint.ConnectionLimit = 100;
                request.ReadWriteTimeout = 30000;
                request.Method = "POST";
                using (Stream newStream = request.GetRequestStream())
                {
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                }
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException we)
                {
                    response = (HttpWebResponse)we.Response;
                }
                using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    result = sr.ReadToEnd();
                }
                response.Close();
            }
            catch (Exception e) { return result = e.Message; }
            finally
            {
                if (request != null)
                    request.Abort();
                GC.Collect();
            }
            return result;
        }
        #endregion
    }
}
